/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reyes.mysite.struts;

import java.util.List;
import java.text.MessageFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import reyes.mysite.jdo.Blog;
import reyes.mysite.dao.GenericDAO;

/**
 *
 * @author reyesyang
 */
public class QueryBlogDetailsForEditAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String nickTitle = request.getParameter("nickTitle");
        GenericDAO<Blog> blogDAO = new GenericDAO<Blog>();

        try {
            String queryString = MessageFormat.format(
                    "select from {0} where nickTitle == {1}",
                    Blog.class.getName(),
                    "'" + nickTitle + "'");

            List<Blog> blogs = blogDAO.query(queryString);
            if (blogs.size() == 1) {
                Blog blog = blogs.get(0);
                request.setAttribute("blog", blog);
            }
        } catch (Exception e) {
            System.out.println("Can't query this blog for editing, Details: " + e.getMessage());
        }

        return mapping.findForward("editBlog");
    }
}
